#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 110;
int f[N][N];
int n, m;
void floyed()
{
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
			}
		}
	 }
}
int main()
{
	while (cin >> n >> m) {
		memset(f, 0x3f, sizeof(f));
		if (n == 0 && m == 0) {
			break;
		}
		while (m--) {
			int a, b, c;
			cin >> a >> b >> c;
			f[a][b] = f[b][a] = min(f[a][b], c);
		}
		floyed();
		cout << f[1][n] << endl;
	}
	return 0;
}